package com.yiren.dbaa.module.exam.api.impl;

import android.app.Activity;

import com.yiren.backstage.config.enums.PlayOrderEnum;
import com.yiren.backstage.config.enums.SiteConfigCodeEnum;
import com.yiren.backstage.currency.bean.VoiceBean;
import com.yiren.backstage.currency.constant.SignalGConstant;
import com.yiren.backstage.currency.entity.GradeStandardEntity;
import com.yiren.backstage.currency.entity.LineInfoEntity;
import com.yiren.backstage.currency.utils.StringUtil;
import com.yiren.backstage.db.dao.cache.GradeStandardCache;
import com.yiren.backstage.player.api.IPlayerCallBack;
import com.yiren.backstage.player.api.PlayerApi;
import com.yiren.dbaa.constant.NumberConstant;
import com.yiren.dbaa.constant.StringConstant;
import com.yiren.dbaa.module.exam.api.IExamGradeApi;
import com.yiren.dbaa.module.exam.api.OnPlayCompleteListener;

import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Describe
 *
 * @author WRX
 * @date 2022/7/19
 */
public class ExamGradeApiImpl implements IExamGradeApi {

    private Activity activity;

    public ExamGradeApiImpl(Activity activity) {
        this.activity = activity;
    }

    @Override
    public List<GradeStandardEntity> getArtificialJudges() {
        List<GradeStandardEntity> artificialJudge = GradeStandardCache.getArtificialJudge();
        if (null != artificialJudge) return artificialJudge;
        else return new ArrayList<>();
    }

    @Override
    public void playGradeVoice(Map<String, GradeStandardEntity> realtimeVoices, OnPlayCompleteListener listener) {
        for (Map.Entry<String, GradeStandardEntity> entity : realtimeVoices.entrySet()) {
            String id = entity.getKey();
            List<VoiceBean> voices = addGradeVoice(entity.getValue());
            listener.onStartPlay(id);
            PlayerApi.playScoreVoice(activity, voices, null);
            break;
        }
    }

    @Override
    public List<VoiceBean> addGradeVoice(GradeStandardEntity gradeStandardEntity) {
        List<VoiceBean> voices = new ArrayList<>();
        String voiceText = StringConstant.PAUSE_DIVIDER;
        if (null != gradeStandardEntity.getProjectEntity()) {
            voiceText += StringUtil.getProjectName(gradeStandardEntity.getProjectEntity().getName());
        } else {
            voiceText += "综合评判";
        }
        voiceText += StringConstant.PAUSE_DIVIDER + gradeStandardEntity.getStandardContent() + StringConstant.PAUSE_DIVIDER;
        voiceText += "扣" + gradeStandardEntity.getGrade() + "分";
        voices.add(new VoiceBean(voiceText, ""));
        return voices;
    }

    @Override
    public void playStartExamVoice(LineInfoEntity lineInfo, IPlayerCallBack iPlayerCallBack) {
        String voiceText = (String) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.L11.toString());
        if (StringUtils.isNotEmpty(voiceText)) {
            String content = voiceText.replaceAll("#", StringConstant.PAUSE_DIVIDER + ((null == lineInfo) ? "" : lineInfo.getName().replace("(高精度)", "")));
            PlayerApi.playTipsVoice(activity, content, iPlayerCallBack);
        } else {
            PlayerApi.playTipsVoice(activity, "", iPlayerCallBack);
        }
    }

    @Override
    public void endExamPlayScore(int totalScore, Map<String, GradeStandardEntity> gradeVoices, boolean isAutoPlay) {
        //播报成绩的顺序(1:先播评分后播成绩 2：先播成绩后播评分)
        if (isAutoPlay) {  //免费播报
            String endExamPlayVoice = (String) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.L20.toString());
            List<VoiceBean> voices = new ArrayList<>();
            voices.add(new VoiceBean(endExamPlayVoice, ""));
            PlayerApi.playScoreVoice(activity, voices, null);
        } else {  //自动评判
            //播报顺序
            PlayOrderEnum playScoreOrder = PlayOrderEnum.valueOf((String) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.L19.toString()));
            //成绩合格的语音
            String passScoreVoice = (String) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.L15.toString());
            //成绩不合格的语音
            String noPassScoreVoice = (String) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.L16.toString());
            //最后播报语音
            String endVoice = (String) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.L17.toString());
            //您的扣分项目是
            VoiceBean scoreHeadVoice = new VoiceBean("您的扣分项目是", "");
            //成绩合格与不合格语音
            VoiceBean scoreQualifiedVoice = new VoiceBean(totalScore >= NumberConstant.PASS_MARK ? passScoreVoice : noPassScoreVoice, "");
            //最后播报语音
            VoiceBean scoreEndVoice = new VoiceBean(endVoice, "");
            //评分列表语音
            List<VoiceBean> voices = new ArrayList<>();
            if (gradeVoices.size() > 0) {
                if (totalScore < NumberConstant.PASS_MARK || (totalScore >= NumberConstant.PASS_MARK && !(boolean) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.L18.toString()))) {
                    for (GradeStandardEntity gradeStandardEntity : gradeVoices.values()) {
                        List<VoiceBean> value = addGradeVoice(gradeStandardEntity);
                        voices.addAll(value);
                    }
                    voices.add(0, scoreHeadVoice);
                }
                switch (playScoreOrder) {
                    case PLAY_POINTS_TO_PLAY_GRADE:
                        voices.add(scoreQualifiedVoice);
                        break;
                    case PLAY_GRADE_TO_PLAY_POINTS:
                        voices.add(0, scoreQualifiedVoice);
                        break;
                }
            } else {
                voices.add(scoreQualifiedVoice);
            }
            voices.add(scoreEndVoice);
            PlayerApi.playScoreVoice(activity, voices, null);
        }
    }

    @Override
    public int getProjectGrade(HashMap<String, Integer> projectCodeMap, GradeStandardEntity gradeStandardEntity) {
        String projectId = gradeStandardEntity.getProjectId();
        int grade;
        if (projectCodeMap.containsKey(projectId)) {
            grade = projectCodeMap.get(projectId) + gradeStandardEntity.getGrade();
        } else {
            grade = gradeStandardEntity.getGrade();
        }
        return grade;
    }

    @Override
    public void resetProjectCode(GradeStandardEntity gradeStandardEntity, HashMap<String, Integer> projectCodeMap) {
        int projectGrade = projectCodeMap.get(gradeStandardEntity.getProjectId());
        int remainGrade = projectGrade - gradeStandardEntity.getGrade(); //获取项目剩余所扣分数
        projectCodeMap.put(gradeStandardEntity.getProjectId(), remainGrade);
        if (remainGrade == 0) {
            projectCodeMap.remove(gradeStandardEntity.getProjectId());
        }
    }
}
